<!DOCTYPE html>
<html>
<head>
    <title>模具备件库存入库</title>
    #parse("sys/header.html")
</head>
<body>
<div id="rrapp" v-cloak>
    <div v-show="showList">
        <Row :gutter="16" style="margin: 8px 0px 0px 1px">
            <div class="search-group">
                <i-col span="4">
                    <i-input placeholder="请扫码型芯条码" v-model="uniqueCode" @keyup.enter.native="scanStockIn" autofocus />
                </i-col>
            </div>
        </Row>
        <Row :gutter="16" style="margin: 3px 0px 0px 0px;">
            <div class="buttons-group">
                <i-button type="error" @click="del"><i class="fa fa-trash-o"></i>&nbsp;删除</i-button>
            </div>
        </Row>
        <table id="jqGrid"></table>
    </div>
</div>

<script>
    $(function () {
        $("#jqGrid").Grid({
            url: '../modulespstock/list?state=0',
            colModel: [
                {label: '仓库', name: 'storeHouseName', index: 'STORE_ID'},
                {label: '备件编码', name: 'partCode', index: 'PART_CODE'},
                {label: '型芯备件码', name: 'uniqueCode', index: 'UNIQUE_CODE'},
                {label: '型芯名称', name: 'sparePartsName', index: 'SPARE_PARTS_NAME'},
                {label: '图号', name: 'sparePartsSpec', index: 'SPARE_PARTS_SPEC'},
                {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            ]
        });

    });

    let vm = new Vue({
        el: '#rrapp',
        data: {
            showList: true,
            title: null,
            uniqueCode: '',// 型芯备件码
            moduleSpStock: {
                remark: '',
                delFlag: '',
                stockOutDate: '',
                stockInDate: '',
                state: '',
                uniqueCode: '',
                purchasingBatchId: '',
                id: '',
            },
            ruleValidate: {
                name: [
                    {required: true, message: '名称不能为空', trigger: 'blur'}
                ]
            },
            q: {
                sparePartsName: ''
            }
        },
        methods: {
            scanStockIn: function (){
                let _this = this;
                if(!this.uniqueCode){
                    iview.Message.error('请扫码！');
                    return;
                }
                Ajax.request({
                    url: "../modulespstock/scanStockIn",
                    params: {'uniqueCode':this.uniqueCode},
                    type: "POST",
                    successCallback: function (r) {
                        vm.reload();
                        // 清空值
                        _this.uniqueCode= '';
                    },
                    failCallback: function (r) {
                        _this.uniqueCode= '';
                    }
                });
            },
            query: function () {
                vm.reload();
            },
            add: function () {
                vm.showList = false;
                vm.title = "新增";
                vm.moduleSpStock = {
                    remark: '',
                    stockOutDate: '',
                    stockInDate: '',
                    state: '',
                    uniqueCode: '',
                    purchasingBatchId: '',
                    id: '',
                };
            },
            update: function (event) {
                let id = getSelectedRow("#jqGrid");
                if (id == null) {
                    return;
                }
                vm.showList = false;
                vm.title = "修改";

                vm.getInfo(id);
            },
            saveOrUpdate: function (event) {
                let url = vm.moduleSpStock.id == (null || '') ? "../modulespstock/save" : "../modulespstock/update";
                Ajax.request({
                    url: url,
                    params: JSON.stringify(vm.moduleSpStock),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function (r) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            },
            del: function (event) {
                let ids = getSelectedRows("#jqGrid");
                if (ids == null) {
                    return;
                }

                confirm('确定要删除选中的记录？', function () {
                    Ajax.request({
                        url: "../modulespstock/delete",
                        params: JSON.stringify(ids),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function () {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                });
            },
            getInfo: function (id) {
                Ajax.request({
                    url: "../modulespstock/info/" + id,
                    async: true,
                    successCallback: function (r) {
                        vm.moduleSpStock = r.moduleSpStock;
                    }
                });
            },
            reload: function (event) {
                // 清空postData缓存数据
                var postData = $("#jqGrid").jqGrid("getGridParam", "postData");
                $.each(postData, function (k, v) {
                    delete postData[k];
                });
                // 构建查询参数
                var param = {};
                if(this.q.sparePartsName){
                    param.sparePartsName = this.q.sparePartsName;
                }
                if(this.q.state){
                    param.state = this.q.state;
                }
                vm.showList = true;
                let page = $("#jqGrid").jqGrid('getGridParam', 'page');
                $("#jqGrid").jqGrid('setGridParam', {
                    postData: param,
                    page: page
                }).trigger("reloadGrid");
            },
            reloadSearch: function () {
                vm.q = {
                    sparePartsName: ''
                };
                vm.reload();
            },
            handleSubmit: function (name) {
                handleSubmitValidate(this, name, function () {
                    vm.saveOrUpdate()
                });
            },
            handleReset: function (name) {
                handleResetForm(this, name);
            }
        }
    });
</script>
</body>
</html>
